package com.polaris.live.ui.main.live

import android.os.Bundle
import android.view.View
import androidx.recyclerview.widget.GridLayoutManager
import com.blankj.utilcode.util.StringUtils
import com.polaris.live.R.drawable
import com.polaris.live.adapter.live.HomeLiveAdapter
import com.polaris.live.cache.CommonCache
import com.polaris.live.common.cacha.DisplayUtil
import com.polaris.live.common.cacha.UserManager
import com.polaris.live.common.constant.AppConst
import com.polaris.live.common.ext.setOnSingleClickListener
import com.polaris.live.common.network.parseState
import com.polaris.live.common.util.ToastGravity
import com.polaris.live.common.util.setFinalVisibility
import com.polaris.live.constant.LiveConst
import com.polaris.live.constant.UserConst
import com.polaris.live.databinding.FragmentLiveListBinding
import com.polaris.live.dialog.common.ConfirmCanalDetailDialog
import com.polaris.live.popup.GlobalPopupManager
import com.polaris.live.resp.back_resp.DisplayUserBean
import com.polaris.live.resp.back_resp.ListWithBannerBean
import com.polaris.live.resp.back_resp.LiveShowMessageResp
import com.polaris.live.translation.R
import com.polaris.live.utils.LiveUtils
import com.polaris.live.utils.StartCallProxy
import com.polaris.live.utils.StartLiveProxy
import com.polaris.live.utils.interfaces.OnAutoRefreshListener
import com.polaris.live.utils.route.RoutePath
import com.polaris.live.utils.route.Router
import com.polaris.live.viewmodel.live.HomeLiveViewModel

/**
 * LiveListFragment
 *
 * @author Created by 半仙 on 2024/2/27/027 16:57
 */
class LiveListFragment : AbstractLiveListFragment<HomeLiveViewModel, FragmentLiveListBinding>(),
    OnAutoRefreshListener {

    override val refreshView by lazy { mBinding.refresh }

    override val layoutManager by lazy {
        GridLayoutManager(context, 2, GridLayoutManager.VERTICAL, false)
    }

    override val mAdapter by lazy { HomeLiveAdapter() }

    override val listLiveData by lazy { mViewModel.mListLiveData }

    override val promptBannerView by lazy { mBinding.bvPrompt }

    private var mLiveShowResp: LiveShowMessageResp? = null

    override fun initView(savedInstanceState: Bundle?) {
        super.initView(savedInstanceState)
        setRefreshTime(FIVE_MINUTES)
        mViewModel.pageType = DisplayUtil.LIVE_LIVE

        mBinding.refresh.setEmptyTip(
            drawable.not_hava_people,
            StringUtils.getString(R.string.none_yet_hava_live)
        )
        mViewModel.livesShowMessage()
        initObserver()
        if (UserManager.getUser().setting?.liveSwitch == null) {
            mBinding.cosLiveTips.visibility = View.GONE
        }
        val isHideTips = CommonCache.getCloseLiveTips()
        if (isHideTips) {
            mBinding.cosLiveTips.setFinalVisibility(View.GONE)
        }
    }

    private fun initObserver() {
        mViewModel.mLiveShowLiveData.observe(this) { resultState ->
            parseState(resultState, {
                mLiveShowResp = it
            })
        }
        mBinding.ivStartLive.setOnSingleClickListener {
            StartLiveProxy.builder(this)
                .startLive(mViewModel, LiveConst.Type.ONLY_LIVE)
        }
        mViewModel.mListLiveData.observe(this) { resultState ->
            parseState(resultState, { resp ->
                if (mViewModel.pageNumber == 0) {
                    val isHaveBanner = resp.list.find { it is ListWithBannerBean }
                    mAdapter.isHaveBanner(isHaveBanner != null)
                }
            })
        }
    }

    override fun initListener() {
        mAdapter.setOnItemClickListener { _, _, position ->
            val displayUserBean = mAdapter.data.getOrNull(position) as? DisplayUserBean?
                ?: return@setOnItemClickListener

            if (displayUserBean.base.living == AppConst.YES) {
                GlobalPopupManager.showCompletePopup(requireContext()) {
                    if (LiveUtils.isToWaiting(displayUserBean)) {
                        StartCallProxy.builder(this).startCall(
                            mViewModel,
                            UserConst.From.LIVE,
                            UserConst.LastFrom.LIVE,
                            LiveConst.SenderFrom.MANUAL,
                            displayUserBean.base.userId, displayUserBean.base
                        )
                    } else {
                        LiveUtils.invokeSpectator(requireContext(), displayUserBean)
                    }
                }
            } else {
                ToastGravity.showGravity(StringUtils.getString(R.string.down_streamed))
            }
        }
        mBinding.cosLiveTips.setOnSingleClickListener {
            ConfirmCanalDetailDialog.show(
                requireContext(),
                mTitle = StringUtils.getString(R.string.prompt),
                mDetail = StringUtils.getString(R.string.if_not_look_live_go_setting_close),
                mConfirm = StringUtils.getString(R.string.go_to_settings),
                mCanal = StringUtils.getString(R.string.cancel),
                onConfirmCallback = { Router.builder(RoutePath.SET_ACTIVITY).navigation(requireActivity()) },
            )
        }
        mBinding.ivFork.setOnSingleClickListener {
            CommonCache.saveCloseLiveTips(true)
            mBinding.cosLiveTips.visibility = View.GONE
        }
    }
}